package cn.edu.sgu.www.mhxysy.service.chongwu.impl;

import cn.edu.sgu.www.mhxysy.entity.chongwu.ChongwuInitSkill;
import cn.edu.sgu.www.mhxysy.mapper.chongwu.ChongwuInitSkillMapper;
import cn.edu.sgu.www.mhxysy.service.chongwu.ChongwuInitSkillService;
import cn.edu.sgu.www.mhxysy.util.CollectionUtils;
import cn.edu.sgu.www.mhxysy.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class ChongwuInitSkillServiceImpl implements ChongwuInitSkillService {

	private final ChongwuInitSkillMapper chongwuInitSkillMapper;

	@Autowired
	public ChongwuInitSkillServiceImpl(ChongwuInitSkillMapper chongwuInitSkillMapper) {
		this.chongwuInitSkillMapper = chongwuInitSkillMapper;
	}

	@Override
	public void save(List<ChongwuInitSkill> list) {
		if (CollectionUtils.isNotEmpty(list)) {
			// 1、删除宠物初始技能
			QueryWrapper<ChongwuInitSkill> queryWrapper = new QueryWrapper<>();

			queryWrapper.eq("chongwu_id", list.get(0).getChongwuId());

			Long count = chongwuInitSkillMapper.selectCount(queryWrapper);

			if (count > 0) {
				chongwuInitSkillMapper.delete(queryWrapper);
			}

			// 2、添加宠物初始技能
			for (ChongwuInitSkill chongwuInitSkill : list) {
				chongwuInitSkill.setId(null);

				chongwuInitSkillMapper.insert(chongwuInitSkill);
			}
		}
	}

	@Override
	public List<ChongwuInitSkill> selectByChongwuId(Integer chongwuId) {
		QueryWrapper<ChongwuInitSkill> queryWrapper = new QueryWrapper<>();

		queryWrapper.eq(
				chongwuId != null,
				"chongwu_id", chongwuId
		);
		queryWrapper.orderByAsc("tag", "id");

		return chongwuInitSkillMapper.selectList(queryWrapper);
	}

}